package com.example.hotHub.controller;

import com.example.hotHub.common.R;
import com.example.hotHub.model.NormalHotSearch;
import com.example.hotHub.service.ImageService;
import com.example.hotHub.utils.RedisUtil;
import com.example.hotHub.utils.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@RequestMapping("test")
@RestController
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ImageService imageService;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @GetMapping("getHotPoint")
    public R getHotPoint(String target) {
        List<NormalHotSearch> list = (List<NormalHotSearch>) redisUtil.get(target);
        List<NormalHotSearch> hotPointList = imageService.getHotPointList(list);
        return R.success(hotPointList);
    }

    @GetMapping("getImage")
    public R getImage(String url) {
        org.springframework.web.client.RestTemplate restTemplate = new org.springframework.web.client.RestTemplate();
        /**
         * 请求地址
         * 请求方式
         * 请求实体类
         * 请求返回类型
         */
        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, byte[].class);
        byte[] body = responseEntity.getBody();
        return R.success(body);
    }

    @GetMapping("download")
    public void getFile(HttpServletResponse response) {
        String url =
                "https://cn-zjjh-ct-04-26.bilivideo.com/upgcxcode/54/77/884337754/884337754_nb3-1-16.mp4?e=ig8euxZM2rNcNbRVhwdVhwdlhWdVhwdVhoNvNC8BqJIzNbfq9rVEuxTEnE8L5F6VnEsSTx0vkX8fqJeYTj_lta53NCM=&uipk=5&nbs=1&deadline=1668095160&gen=playurlv2&os=bcache&oi=462898447&trid=0000da602aafe00845d983108c4a9e6d42e3h&mid=0&platform=html5&upsig=4f66cfd8187ab5d9cc89065a85019475&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&cdnid=6584&bvc=vod&nettype=0&bw=28872&logo=80000000";
        try {
            // path是指欲下载的文件的路径。
            File file = new File(url);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(url));

            byte[] buffer = new byte[fis.available()];

            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();

            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");

            /* 与上等同
            HttpHeaders headers = new HttpHeaders();
            //将编码加到http头信息中
            headers.setContentDispositionFormData("attachment", filename);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.add("isSuccess", "true");
            return new ResponseEntity<>(file, headers, HttpStatus.CREATED);
            */

            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
//        return (ResponseEntity) response;
    }

}
